package com.xuchy.algorithm;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * @author xuchy
 * @Date 2019/11/5 23:22
 */
public class TablePreciseShardAlgorithm implements PreciseShardingAlgorithm<Date> {

    private static SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        //分片键的值
        Date shardingColumnValue = preciseShardingValue.getValue();
        String dateStr = sf.format(shardingColumnValue);
        //逻辑表名
        String logicTableName = preciseShardingValue.getLogicTableName();
        //组装物理表名
        String tbname = logicTableName + "_" + dateStr;
        System.out.println("----------------tbname:" + tbname);
        return tbname;
    }

}
